﻿package Player
{
	import Events.SnakeChangeEvent;
	
	import Views.SnakeView;
	
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.ui.Keyboard;
	import flash.utils.getTimer;
	import flash.utils.Timer;
	import flash.events.Event;

	/**
	 * Class to handle player.
	 * Contains Status : normal, inverseControls, dead
	 * Saves left and right key control
	 * Saves the corner radius when turning
	 */
	public class PlayerModel
	{
		public static const NORMAL:String = "normal"; 
		public static const INVERTED:String = "inverted"; 
		public static const DEAD:String = "dead";
		
		private var _name:String;
		private var _leftKey:uint = Keyboard.LEFT;
		private var _rightKey:uint =  Keyboard.RIGHT;
		
		// Control variables
		private var leftKeyDown:Boolean;
		private var rightKeyDown:Boolean;
		private var controlInverted:Boolean;
		
		private var _status:String;
		private var _curveRadius:Number;
		private var _snake:SnakeHead;
		
		// Timer to make snake move
		private const timerDelay:Number = (1/60)*1000;
		private var lastTime:Number;
		private var _timer:Timer;
		
		public function PlayerModel(snake:SnakeHead, name:String = "Willem")
		{
			_name = name;
			_snake = snake;
			_curveRadius = 1;
			_status = PlayerModel.NORMAL;
			
			// Initialize controlInverted
			controlInverted = false;
			
			// Set lastTime for reference
			_timer = new Timer(timerDelay);
			_timer.addEventListener(TimerEvent.TIMER, frameUpdate);
		}
		
		/**
		* Start timer and store lastTime in milliseconds
		*/
		public function startPlayer():void{
			_timer.start();
			lastTime = getTimer();
		}
		
		public function stopPlayer():void{
			_timer.stop();
		}
		
		public function resetPlayer():void{
			
		}
		
		public function frameUpdate(timerEvent:TimerEvent):void{
			// Get elapsed time in seconds
			var newTime:int = getTimer();
			var elapsedTime:int = newTime - lastTime;
			lastTime = newTime;
			var elapsedSeconds:Number = elapsedTime/1000;
			
			// Change snake orientation
			if(leftKeyDown || rightKeyDown){
				updateOrientation(elapsedSeconds);
			}
			
			// Update the snake movement
			_snake.updateSnake(elapsedSeconds);
		}
		
		/**
		 * Function to change snake orientation on key down
		 */
		private function updateOrientation(timePassed:Number):void{
			if(leftKeyDown && rightKeyDown){
				_snake.turnSnakeLeft(timePassed);
			}
			if(leftKeyDown){
				_snake.turnSnakeLeft(timePassed);
			}
			if(rightKeyDown){
				_snake.turnSnakeRight(timePassed);
			}
		}
		
		/**
		 * Function to handle player dying. Just stop movement for now
		 */
		public function die():void{
		}

		public function getSnake():SnakeHead{
			return _snake;
		}
		
		public function isLeftKey(key:uint):Boolean{
			if(controlInverted){
				return isRightKey(key);
			}
			return _leftKey == key;
		}
		
		public function isRightKey(key:uint):Boolean{
			if(controlInverted){
				return isLeftKey(key);
			}
			return _rightKey == key;
		}
		
		public function set leftKey(leftKey:uint):void{
			_leftKey = leftKey;
		}
		public function set rightKey(rightKey:uint):void{
			_rightKey = rightKey;
		}
		
		public function set leftKeyStatus(down:Boolean):void{
			leftKeyDown = down;
		}
		public function set rightKeyStatus(down:Boolean):void{
			rightKeyDown = down;
		}
		
		public function set curveRadius(curveRadius:Number):void{
			_curveRadius = curveRadius;
		}
	}
}